package com.weixing.mall.provider.support.excalservice;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.fastjson.JSON;
import com.weixing.mall.provider.model.domain.ProductStockDetails;
import com.weixing.mall.provider.model.vo.ProductStockListVo;
import com.weixing.mall.provider.service.IProductStockDetailsService;
import com.weixing.mall.provider.support.excaldata.ProductStockDetailsListED;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @program: mall-master
 * @description:
 * @author: wangfang
 * @create: 2022-06-07 22:48
 **/

public class ProductStockDetailsDataListener extends AnalysisEventListener<ProductStockDetailsListED> {
    private static final Logger LOGGER = LoggerFactory.getLogger(ProductStockDetailsDataListener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;

    List<ProductStockDetailsListED> list = new ArrayList<ProductStockDetailsListED>();

    private IProductStockDetailsService productStockService;
    /**
     * 业务数据组装
     */
    private List<ProductStockListVo> returnList;


    private ProductStockDetailsDataListener() {

    }

    public ProductStockDetailsDataListener(IProductStockDetailsService productStockService) {

        this.productStockService = productStockService;
    }

    /**
     * 这个每一条数据解析都会来调用
     *
     * @param productStockED
     * @param analysisContext
     */
    @Override
    public void invoke(ProductStockDetailsListED productStockED, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", JSON.toJSONString(productStockED));
        list.add(productStockED);
    }


    /**
     * 所有数据解析完成了 都会来调用
     *
     * @param analysisContext
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        LOGGER.info("{}条数据，开始处理数据！", list.size());
        for (ProductStockDetailsListED ed : list) {
            try {
                Map<String, Object> map = productStockService.test(ed.getColor().trim(), ed.getSize().trim(), ed.getStyleNo().trim());
                ProductStockDetails details = new ProductStockDetails();
                if (ObjectUtil.isNotNull(map)) {
                    String markPrice = ObjectUtil.isNull(map.get("market_price")) ? "0" : map.get("market_price").toString();
                    details.setMarkerPrice(new BigDecimal(markPrice));
                    String disPrice = ObjectUtil.isNull(map.get("discount_price")) ? "0" : map.get("discount_price").toString();
                    details.setStockPrice(new BigDecimal(disPrice));
                    details.setStockSn(ed.getStockSn());
                    details.setProductId(Long.valueOf(map.get("id").toString()));
                    details.setProductSn(map.get("product_sn").toString());
                    details.setGoodsSn(map.get("goods_sn").toString());
                    details.setGoodsId(Long.valueOf(map.get("goods_id").toString()));
                    details.setCreatedTime(new Date());
                    details.setCreatedBy(1L);
                    details.setDepotId(ed.getDepotId());
                    details.setProductSkuDesp(map.get("product_sku_desp").toString());
                    details.setProductImg(map.get("product_img").toString());
                    details.setStyleNo(map.get("style_no").toString());
                    details.setProductName(map.get("product_name").toString());
                    productStockService.save(details);
                } else {
                    System.out.println("有错误");
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        LOGGER.info("{}条数据，数据封装处理完成 ", list.size());
    }

    /**
     * 获取处理结果数据
     */
    public List<ProductStockListVo> getReturnList() {
        return this.returnList;
    }

}